<template>
	<view class="container">
		<u-toast ref="uToast" />
		<u-navbar :is-back="false" title="我的课组" back-icon-color='#333333' height="50" :border-bottom="false">
			<text class="iconfont icon-fanhuijian2" style="font-size:30rpx; margin-left:30rpx; font-weight:bold;"
				@click="previous_pages"></text>
		</u-navbar>
		<!-- 课租列表 -->
		<view class="list">
			<view class="group_list" v-for="(item,index) in groupList" :key="index"
				@click="$toLink('../research_details/research_details',{id:item.name,subject_id:item.subject_id})">
				<view class="left">
					<image :src="item.img" mode="widthFix"></image>
				</view>
				<view class="center">
					<view class="top">
						<text>{{item.name}}</text>
					</view>
					<view class="bottom">
						<image src="../../static/tupian114.png" mode="widthFix"></image>
						<text>{{item.people_count}}人</text>
					</view>
				</view>
				<view class="right">
					<image src="../../static/tupian93.png" mode="widthFix"></image>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" />
	</view>
</template>
<script>
	import request from '../../request.js'
	export default {
		data() {
			return {
				group_list: [],
				back_page: 1,
				status: 'loading',
				page: 0,
				last_page: '',
			}
		},
		onLoad(option) {
			if (option.id) {
				switch (option.id) {
					case 1:
						this.back_page = 1;
						break;
					case 2:
						this.back_page = 2;
						break;
					case 3:
						this.back_page = 3;
						break;
				}
			}
			this.myGroup();
		},
		computed: {
			groupList() {
				if (this.group_list) {
					return this.group_list;
				} else {
					return [];
				}
			}
		},
		onReachBottom() { //滚动到底部触发该函数
			console.log('触发了吗')
			if (this.page >= this.last_page) {
				this.status = 'nomore';
				return
			} else {
				this.status = 'loading';
			} //如果大于最后一页那么就中断函数

			this.page = ++this.page;

			setTimeout(() => {
				this.myGroup(this.page);
			}, 500)
		},
		methods: {
			myGroup(param) {
				let base_url = uni.getStorageSync('host');
				console.log(base_url);
				let _this = this;
				let pagination = param ? param : 1;
				request({
					url: base_url + 'teacher/group/myGroup',
					method: 'post',
					data: {
						page: pagination,
					},
					success(res) {
						if (res.statusCode == 200) {
							console.log(res);
							console.log(res.data.data.data, res.data.data.last_page);
							_this.last_page = res.data.data.last_page;
							if (res.data.data.total == 0) {
								console.log('又触发吗');
								_this.status = 'nomore'
								//如果接口返回的数据为零的话，那么就中断函数
								_this.group_list.length = 0;
								//没有数据的话那么就将数组清空
								_this.group_list = [];
								return;
							};
							//页数为1的情况
							if (pagination == 1) {
								//先将原数组清空
								_this.group_list = [];
								console.log('进来了吗');
								if (res.data.data.data == null) {
									console.log('执行到这了吗')
									_this.group_list = [];
								} else {
									_this.group_list = res.data.data.data;
								}
								console.log(_this.group_list);
								_this.page = 1;
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
								//页数大于1的情况
							} else if (pagination > 1) {
								console.log(res.data.data.data);
								if (res.data.data.data == null) {
									_this.status = 'nomore'
								}
								_this.group_list = [..._this.group_list, ...res.data.data.data];
								console.log(_this.group_list);
								if (pagination >= _this.last_page) {
									_this.status = 'nomore'
									return
								}
							}
							console.log(_this.group_list);
						} else {
							_this.$refs.uToast.show({
								title: res.data.message || '系统错误',
								type: 'warning',
							})
						}

					}
				})
			},
			//对返回上一级页面分情况跳转
			previous_pages() {
				switch (this.back_page) {
					case 1:
						this.$toLink('../career/career', {}, 3)
						break;
					case 2:
						uni.navigateBack({
							delta: 1
						})
						break;
					case 3:
						break;
				}
			},
		}


	}
</script>

<style lang="scss">
	.container {
		width: 100%;
		overflow: hidden;
		background: #F2F2F2;
		height: 100vh;

		.list {
			width: 100%;
			overflow: hidden;

			.group_list {
				box-shadow: 0px 1px 1px 1px #ccc;
				width: 710rpx;
				height: 130rpx;
				margin-left: 20rpx;
				background: #fff;
				display: flex;
				align-items: center;
				justify-content: flex-start;
				border-radius: 12rpx;
				margin-top: 20rpx;

				.left {
					width: 72rpx;
					margin-left: 20rpx;

					image {
						&:nth-child(1) {
							width: 70rpx;
						}
					}
				}

				.center {
					width: 520rpx;
					margin-left: 20rpx;
					height: 90rpx;
					overflow: hidden;

					.top {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: flex-start;
						font-size: 30rpx;
						color: #333333;
					}

					.bottom {
						width: 100%;
						margin-top: 20rpx;
						height: 35rpx;
						display: flex;
						align-items: center;
						justify-content: flex-start;

						image {
							&:nth-child(1) {
								width: 30rpx;
							}
						}

						text {
							&:nth-child(2) {
								color: #333333;
								margin-left: 20rpx;
								font-size: 26rpx;
							}
						}
					}
				}

				.right {
					margin-left: 30rpx;

					image {
						&:nth-child(1) {
							width: 20rpx;
						}
					}
				}
			}
		}
	}
</style>
